#initial position of our super cool block
xpos:
.word 0

ypos:
.word 0

#do all the stuff
main_loop:

	move $a0, $s0
#	jal libplp_timer_wait
#	nop

	# frame buffer	
	li $t0, fb_1
	lw $t0, 0($t0)
	beq $a3, $t0, draw_fb2
	nop
	li $t0, fb_1
	j fb_done
	nop
draw_fb2:
	li $t0, fb_2

fb_done:
	lw $a3, 0($t0)

	#li $a0, 0x00000000
	#move $a1, $a3
	# jal libplp_vga_clear2
	nop
	
	# moving the block
	# Loads the ypos into t0
	li $t0, ypos
	lw $t1, 0($t0)
	# move down by 4 pixels
	# we use 4 pixels to retain alignment
	addiu $t1, $t1, 4
	# save position
	sw $t1, 0($t0)

	li $t1, ypos
	lw $t1, 0($t0)
	addiu $t1, $t1, 4
	sw $t1, 0($t0)
	
	# draw block
	li $a0, 0xe0e0e0e0
	# store positions into registers
	li $a1, xpos
	li $a2, ypos
	# actually draw it
	jal draw_square
	nop


#	# erase block
#	li $a0, 0x00000000
#	li $a1, xpos
#	li $a2, ypos
#	jal draw_square2
#	nop

	move $a0, $a3
	jal libplp_vga_framebuffer
	nop

	j main_loop
	nop


draw_square:

	# memory map for VGA RTFM
	lui $t0, 0xf040
	move  $t1, $a3

	#load the position of the block into the vga register
	lw $t2, 0($a1)	# t2 has x
	lw $t3, 0($a2)	# t3 has y
	# block dimensions
	addiu $t4, $t2, 60	# t4 has x limit 
	addiu $t5, $t3, 80	# t5 has y limit

loop_1:
	# fills pixels along y-asis until limit reached
	# jumps to main_loop when done
	slt $t6, $t3, $t5 # moving across y-axis
	beq $zero, $t6, loop_1_done 
	nop

loop_2:
	slt $t6, $t2, $t4
	beq $zero, $t6, loop_2_done
	nop

# Start of stuff we don't understand at all

	sll $t7, $t3, 9	# multiply y by 512 store in t7
	sll $t8, $t3, 7	# multiply y by 128 store in t8
	addu $t7, $t7, $t8	# t7 = t7 + t8
	addu $t7, $t7, $t2	# t7 = t7 + xpos
	addu $t7, $t7, $t1	# t7 = t7 + location of start of blk

	sw $a0, 0($t7)	# store t7 into a0

	addiu $t2, $t2, 4	# increment 2 by 4
	j loop_2
	nop

loop_2_done:
	addiu $t3, $t3, 1	# t3++
	lw $t2, 0($a1)	# t2 = xpos
	j loop_1
	nop
	
loop_1_done:
	jr $ra
	nop



draw_square2:
	lui $t0, 0xf040
	move $t1, $a3

	lw $t2, 0($a1)
	lw $t3, 0($a2)
	
	addiu $t4, $t2, 60	# t4 has x limit 
	addiu $t5, $t3, 80	# t5 has y limit
	
	j loop_1a
	nop 


loop_1a:
	# fills pixels along y-asis until limit reached
	# jumps to main_loop when done
	slt $t6, $t3, $t5 # moving across y-axis
	beq $zero, $t6, loop_1a_done 
	nop

loop_2a:
	slt $t6, $t2, $t4
	beq $zero, $t6, loop_2a_done
	nop

# Start of stuff we don't understand at all

	sll $t7, $t3, 9
	sll $t8, $t3, 7
	addu $t7, $t7, $t8
	addu $t7, $t7, $t2
	addu $t7, $t7, $t1

	sw $a0, 0($t7)

	addiu $t2, $t2, 4
	j loop_2a
	nop

loop_2a_done:
	addiu $t3, $t3, 1
	lw $t2, 0($a1)
	j loop_1a
	nop
	
loop_1a_done:
	jr $ra
	nop

# end of stuff we don't understand at all



